home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CExamples / TubeTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  13.7 KB  |  452 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Animation Sample Application
  6. #
  7. #    TubeTest
  8. #
  9. #    TubeTest.c    -    C Source
  10. #
  11. #    Copyright © 1988, 1994-95 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #                1.01                3/94    Updated for Universal Interfaces
  16. #
  17. #    Components:    TubeTest.c            August 1, 1988
  18. #                TubeTest.r            August 1, 1988
  19. #                TubeTest.make        August 1, 1988
  20. #
  21. #    The TubeTest program is a simple demonstration of how to use the Palette 
  22. #    Manager in a color program.  It has a special color palette that is associated
  23. #    with the main window.  The colors are animated using the Palette Manager 
  24. #    to give a flowing tube effect.  The program is very simple, and the Palette
  25. #    Manager and drawing parts are put in separate subroutines to make it easier
  26. #    to figure out what is happening.
  27. #    
  28. #    The program is still a complete Macintosh application with a Main Event Loop,
  29. #    so there is the extra code to run the MEL.  
  30. #    
  31. #    There is a resource file that is necessary as well, to define the Menus, Window,
  32. #    Dialog, and Palette resources used in the program.  
  33. #
  34. #    See Sample and TESample for the general structure and MultiFinder techniques that
  35. #    we recommend that you use when building a new application.
  36. #
  37. ------------------------------------------------------------------------------*/
  38.  
  39.  /*
  40.     File TubeTest.c
  41.  
  42.     Version 1.0:  6/2/88
  43.                  7/20/88    DJB    Converted to C (MPW 3.0 ONLY)
  44.             1.01 9/16/92    GAB Replaced GetNextEvent call with WaitNextEvent
  45.     
  46.     4/19/88:
  47.     TubeTest -- A small sample application written by Bo3b Johnson.
  48.     The idea is to draw two circles in varying colors in the window, then
  49.     animate the colors when the menu is chosen.    This is a complete program
  50.     with event loop and everything.    It is intended to be a simple example of
  51.     how to use the Palette Manager to do some minor color animation, and
  52.     how to use the PM to set up the colors desired in a window.
  53.     
  54.     Also see the resource file that goes with this to see how the Palette 
  55.     itself is layed out.
  56.     
  57.     Could be built with something like this:
  58.         rez TubeTest.r -o TubeTest
  59.         C -r TubeTest.C
  60.         Link TubeTest.c.o ∂
  61.             "{Libraries}"Interface.o ∂
  62.             "{Libraries}"Runtime.o ∂
  63.             -o TubeTest
  64.         TubeTest
  65.  */
  66.  
  67. /* Where does it fit:
  68.     This is a series of sample programs for those doing development
  69.     using Color QuickDraw.  Since the whole color problem depends
  70.     upon the exact effect desired, there are a number of answers
  71.     to how to use colors, from the simple to the radically complex.
  72.     These programs try to cover the gamut, so you should use 
  73.     which ever seems appropriate.  In most cases, use the simplest
  74.     one that will give the desired results.  The compatibility
  75.     rating is from 0..9 where low is better.  The more known risks 
  76.     there are the higher the rating.
  77.     
  78.     
  79.     The programs (in order of compatibility):
  80.     
  81.         SillyBalls:
  82.             This is the simplest use of Color QuickDraw, and does
  83.             not use the Palette Manager.  It draws randomly colored
  84.             balls in a color window.  This is intended to give you
  85.             the absolute minimum required to get color on the screen.
  86.             Written in straight Pascal code.
  87.             Compatibility rating = 0, no known risks.
  88.         
  89.         FracAppPalette:
  90.             This is a version of FracApp that uses only the Palette
  91.             Manager.  It does not support color table animation
  92.             since that part of the Palette Manager is not sufficient.
  93.             The program demonstrates a full color palette that is
  94.             used to display the Mandelbrot set.  It uses an offscreen
  95.             gDevice w/ Port to handle the data, using CopyBits to
  96.             draw to the window.  The Palette is automatically 
  97.             associated with each window.  The PICT files are read
  98.             and written using the bottlenecks, to save on memory
  99.             useage.
  100.             Written in MacApp Object Pascal code.
  101.             Compatibility rating = 0, no known risks.
  102.         
  103.         TubeTest:    (***)
  104.             This is a small demo program that demonstrates using the
  105.             Palette Manager for color table animation.  It uses a 
  106.             color palette with animating entries, and draws using the
  107.             Palette Manager.  There are two circles of animating colors
  108.             which gives a flowing tube effect.  This is a valid case
  109.             for using the animating colors aspect of the Palette Manager,
  110.             since the image is being drawn directly.
  111.             Written in straight Pascal code.
  112.             Compatibility rating = 0, no known risks.
  113.         
  114.         FracApp:
  115.             This is the ‘commercial quality’ version of FracApp.  This
  116.             version supports color table animation, using an offscreen
  117.             gDevice w/ Port, and handles multiple documents.  The
  118.             CopyBits updates to the screen are as fast as possible.  The
  119.             program does not use the Palette Manager, except to
  120.             provide for the system palette, or color modes with less than
  121.             255 colors.  For color table animation using an offscreen
  122.             gDevice w/ Port, it uses the Color Manager and handles the
  123.             colors itself.  Strict compatibility was relaxed to allow for
  124.             a higher performance program.  This is the most ‘real’ of the
  125.             sample programs.
  126.             Written in MacApp Object Pascal code.
  127.             Compatibility rating = 2.  (nothing will break, but it may not
  128.                 always look correct.)
  129.         
  130.         FracApp300:
  131.             This doesn't support colors, but demonstrates how to create and
  132.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  133.             resolution on LaserWriters, and clipped on other printers (but
  134.             they still print).  It demonstrates how to use a high resolution
  135.             image as a PICT file, and how to print them out.
  136.             Written in MacApp Object Pascal code.
  137.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  138.                 out of the ordinary, although supported.)
  139. */
  140.  
  141.  
  142. #include <Types.h>
  143. #include <Memory.h>
  144. #include <Quickdraw.h>
  145. #include <Palettes.h>
  146. #include <Fonts.h>
  147. #include <Windows.h>
  148. #include <TextEdit.h>
  149. #include <Dialogs.h>
  150. #include <Menus.h>
  151. #include <Devices.h>
  152. #include <Events.h>
  153. #include <Sound.h>
  154. #include <ToolUtils.h>
  155. #include <Processes.h>
  156.     
  157. /* Constants */
  158. #define appleID                1000            /* resource IDs/menu IDs for Apple, */
  159. #define fileID                1001            /*     File and */
  160. #define editID                1002            /*    Edit menus */
  161.     
  162. #define appleM                0                /* Index for each menu in myMenus (array of menu handles) */
  163. #define fileM                1
  164. #define editM                2
  165.  
  166. #define menuCount            3                /* Total number of menus */
  167.  
  168. #define windowID            1000            /* Resource ID for main window */
  169. #define aboutMeDLOG         1000            /* And Resource ID for About box dialog. */
  170.  
  171. #define tubularItem            1                /* When checked, animation of colors. */
  172. #define quitItem            3                /* Quit in the menu of course. */
  173.  
  174. #define aboutMeCommand        1                /* Menu item in apple menu for About TubeTest item */
  175.     
  176. #define totalColors            152                /* use 150 colors in our palette for drawing eyes. */
  177. #define numColors            150                /* to skip black and white. */
  178.     
  179.     
  180. /* Globals */
  181. MenuHandle    myMenus[menuCount];
  182. Rect        dragRect;                        /* Rectangle used to mark bounds for dragging window */
  183. Boolean        doneFlag,                        /* true if user has chosen Quit command */
  184.             tubeCheck;                        /* if true, the menu is checked, and we animate. */
  185. EventRecord    myEvent;
  186. WindowPtr    myWindow,
  187.             whichWindow;
  188. char        theChar;
  189. OSErr        error;
  190. SysEnvRec    theWorld;
  191. QDGlobals    qd;
  192.  
  193. /* Prototypes */
  194. void DrawEyes();
  195. void SetUpMenus();
  196. void ShiftyColors();
  197. void ShowAboutMeDialog();
  198. void DoCommand(long int mResult);
  199.  
  200.  
  201.     
  202. void main(void)
  203. {
  204.     /*
  205.     **    Test the computer to be sure we can do color.  
  206.     **    If not we would crash, which would be bad.  
  207.     **    If we can’t run, just beep and exit.
  208.     */
  209.     error = SysEnvirons(1, &theWorld);
  210.     if (theWorld.hasColorQD == false) {
  211.         SysBeep (50);
  212.         ExitToShell();                            /* If no color QD, we must leave. */
  213.     };
  214.  
  215.     InitGraf(&qd.thePort);
  216.     InitFonts();
  217.     InitWindows();
  218.     InitMenus();
  219.     TEInit();
  220.     InitDialogs(nil);
  221.     InitCursor();
  222.  
  223.     SetRect(&dragRect, 4, 24, qd.screenBits.bounds.right - 4, qd.screenBits.bounds.bottom - 4);
  224.     doneFlag = false;                            /* flag to detect when Quit command is chosen */
  225.     tubeCheck = false;                            /* flag for animating color is initially off. */
  226.  
  227.     /*
  228.     **    Open the color window.
  229.     */
  230.     myWindow = GetNewCWindow(windowID, nil, (WindowPtr) -1);
  231.     SetPort(myWindow);
  232.  
  233.     /*
  234.     **    Set up menus last, since the menu drawing can then use 
  235.     **    the palette we have for our window. 
  236.     **    Makes the Apple look better, in particular.
  237.     */
  238.     SetUpMenus();
  239.     
  240.     /*
  241.     **    Main Event Loop
  242.     */
  243.     do {
  244.         SystemTask();
  245.  
  246.         if (WaitNextEvent(everyEvent, &myEvent, 5L, NULL)) {
  247.             switch (myEvent.what) {                /* case on event type */
  248.  
  249.                 case mouseDown:
  250.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  251.  
  252.                         case inSysWindow:        /* desk accessory window: call Desk Manager to handle it */
  253.                             SystemClick(&myEvent, whichWindow);
  254.                             break;
  255.  
  256.                         case inMenuBar:            /* Menu bar: learn which command, then execute it. */
  257.                             DoCommand(MenuSelect(myEvent.where));
  258.                             break;
  259.  
  260.                         case inDrag:            /* title bar: call Window Manager to drag */
  261.                             DragWindow(whichWindow, myEvent.where, &dragRect);
  262.                             break;
  263.  
  264.                         case inContent:            /* body of application window: */
  265.                             if (whichWindow != FrontWindow())
  266.                                 SelectWindow(whichWindow); /* and make it active if not */
  267.                             break;
  268.                     }
  269.                     break;
  270.  
  271.                 case updateEvt:                    /* Update the eyes in window. */
  272.                     if ((WindowPtr) myEvent.message == myWindow) {
  273.                         BeginUpdate((WindowPtr) myEvent.message);
  274.                         DrawEyes();
  275.                         EndUpdate((WindowPtr) myEvent.message);
  276.                     }
  277.                     break;
  278.                             
  279.                 case keyDown:
  280.                 case autoKey:                    /* key pressed once or held down to repeat */
  281.                     if (myWindow == FrontWindow()) {
  282.                         theChar = (myEvent.message & charCodeMask); /* get the char */
  283.                         /* 
  284.                         **    If Command key down, do it as a Menu Command.
  285.                         */
  286.                         if (myEvent.modifiers & cmdKey)
  287.                             DoCommand(MenuKey(theChar));
  288.                     }
  289.                     break;
  290.  
  291.             }
  292.         }
  293.  
  294.         /*    
  295.         **    If we have menu item checked, go ahead and animate colors.
  296.         */
  297.         if (tubeCheck) ShiftyColors();
  298.         
  299.     } while (!doneFlag);
  300.  
  301.     /*
  302.     **    clean up after palette manager, 
  303.     **    so he can chuck the palette in use.
  304.     */
  305.     DisposeWindow (myWindow);
  306. }
  307.  
  308. /*
  309. **    This routine will update the window when required by update events.    It
  310. **    will draw two circular dudes that are indexed in colors through the colors
  311. **    we are using. 0 and 1 are skipped, since those are white and black in the
  312. **    palette.
  313. */
  314. void DrawEyes(void)
  315. {
  316.     Rect    tempRect;
  317.     int        i;
  318.     
  319.     SetRect(&tempRect, numColors, numColors, numColors, numColors);
  320.     for (i = 2; i <= totalColors; i++) {
  321.         PmForeColor(i);
  322.         FrameOval (&tempRect);
  323.         InsetRect (&tempRect, -1, -1);
  324.     }
  325.     
  326.     SetRect(&tempRect, numColors*3, numColors, numColors*3, numColors);
  327.     for (i = totalColors; i >= 2; i--) {
  328.         PmForeColor(i);
  329.         FrameOval (&tempRect);
  330.         InsetRect (&tempRect, -1, -1);
  331.     }
  332. }
  333.  
  334.  
  335. /*
  336. **    Read menu descriptions from resource file into memory 
  337. **    and store handles in myMenus array.
  338. **    Insert into MenuBar and draw.
  339. */
  340. void SetUpMenus(void)
  341. {
  342.     int i;
  343.  
  344.     myMenus[appleM] = GetMenu(appleID);         /* read Apple menu from resource file */
  345.     AppendResMenu(myMenus[appleM], 'DRVR');        /* add desk accessory names to Apple menu */
  346.     myMenus[fileM] = GetMenu(fileID);            /* read file menu from resource file */
  347.     myMenus[editM] = GetMenu(editID);            /* read edit menu from resource file */
  348.  
  349.     for (i = 0; i < menuCount; i++) 
  350.         InsertMenu(myMenus[i], 0);                 /* install menus in menu bar */
  351.     
  352.     DrawMenuBar();                                /* and draw menu bar */
  353. }
  354.  
  355.  
  356. /*
  357. **    Use the Palette currently attached to the main window to animate the colors 
  358. **    in the circular eye shapes.  This will rotate them around to give the flowing 
  359. **    tube effect. We make the palette into a color table so we can move entries 
  360. **    around.    We have to skip the first two entries since those are black and white. 
  361. **    (entries 0 and 1)
  362. */
  363. void ShiftyColors(void)
  364. {
  365.     
  366.     PaletteHandle    currPalette;
  367.     CTabHandle        destCTab;
  368.     ColorSpec        lastCSpec;
  369.     
  370.     SetPort (myWindow);
  371.     
  372.     currPalette = GetPalette(myWindow);
  373.     destCTab = (CTabHandle) NewHandle(sizeof(ColorTable)+(totalColors*sizeof(ColorSpec)));
  374.     if (destCTab == nil)  return;
  375.     Palette2CTab(currPalette, destCTab);
  376.     
  377.     /*
  378.     **    Move the colors around in the color table, skipping 0 and 1, and moving
  379.     **    all the elements down by one, and copying the element at 2 back to the 
  380.     **    end of the table. The effect is to rotate the colors in the table.
  381.     */
  382.     lastCSpec = (*destCTab)->ctTable[2];                        /* pull first one off. */
  383.     BlockMove (&(*destCTab)->ctTable[3], 
  384.                &(*destCTab)->ctTable[2], 
  385.                (numColors) * sizeof(ColorSpec) );                /* copy all one entry down. */
  386.     (*destCTab)->ctTable[totalColors-1] = lastCSpec;            /* put last color back on front. */
  387.         
  388.     AnimatePalette(myWindow, destCTab, 2, 2, numColors);
  389.     
  390.     DisposeHandle ((Handle) destCTab);
  391. }
  392.  
  393.  
  394. /*    
  395. **    Display the dialog box in response to the 'About TubeTest' menu item
  396. */
  397. void ShowAboutMeDialog(void)
  398. {
  399.     DialogPtr    theDialog;
  400.     short        itemHit;
  401.  
  402.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  403.     ModalDialog(nil, &itemHit);
  404.     DisposeDialog(theDialog);
  405. }
  406.  
  407.  
  408. /*
  409. **    Execute menu command specified by mResult,
  410. **    the result of MenuSelect
  411. */
  412. void DoCommand(long int mResult)
  413. {
  414.     short    theItem,                            /* menu item number from mResult low-order word */
  415.             theMenu;                            /* menu number from mResult high-order word */
  416.     Str255    name;                                /* desk accessory name */
  417.     int        temp;
  418.     Boolean    dummy;
  419.  
  420.     theItem = LoWord(mResult);                    /* call Toolbox Utility routines to */
  421.     theMenu = HiWord(mResult);                    /* set menu item number and menu */
  422.  
  423.     switch (theMenu) {                            /* switch on menu ID */
  424.  
  425.         case appleID:
  426.             if (theItem == aboutMeCommand)
  427.                 ShowAboutMeDialog();
  428.             else {
  429.                     GetMenuItemText(myMenus[appleM], theItem, name);
  430.                     temp = OpenDeskAcc(name);
  431.                     SetPort(myWindow);
  432.             }
  433.             break;
  434.  
  435.         case fileID:
  436.             if (theItem == quitItem)
  437.                 doneFlag = true;
  438.             else if (theItem == tubularItem) {
  439.                     tubeCheck = !tubeCheck;
  440.                     CheckItem(myMenus[fileM], tubularItem, tubeCheck);
  441.             }
  442.             break;
  443.  
  444.         case editID:
  445.             dummy = SystemEdit(theItem - 1);    /* Pass the command on to the Desk Manager. */
  446.             break;
  447.     }
  448.  
  449.     HiliteMenu(0);                                /* Unhighlight menu title */
  450.                                                 /* (highlighted by MenuSelect) */
  451. }
  452.